MPT.addAction("editProfile", function(_elm) {
	//收起框
	var infoBar_jQ = $('.e_infoBar',_elm);
	//取消按钮
	var cancel_jQ = $('.e_cancel',_elm);
	
	//隐私信息
	var privacy = {
		"all" : 0,    //完全公开
		"only" : 1,   //好友公开
		"secrecy" : 2 //保密
	}
	var reverPrivacy = ['all','only','secrecy'];
	var privacyText = ['完全公开','好友公开','保密'];
	
	//基本信息
	var basicInfo_jQ = $('.e_basicinfo',_elm);
	var basicInfo = {
		sex_jQ :  $('.e_sex',basicInfo_jQ),//性别
		birth_year_jQ : $('.e_birth_year',basicInfo_jQ),//生日年份
		birth_month_jQ : $('.e_birth_month',basicInfo_jQ),//生日月份
		birth_date_jQ : $('.e_birth_date',basicInfo_jQ),//生日日期
		liveProvince_jQ : $('.e_liveProvince',basicInfo_jQ),//现居住省份
		liveCity_jQ : $('.e_liveCity',basicInfo_jQ),//现居住城市
		homeProvince_jQ : $('.e_homeProvince',basicInfo_jQ),//家乡省份
		homeCity_jQ : $('.e_homeCity',basicInfo_jQ),//家乡城市
		basicSave_jQ : $('.e_basic_save',_elm),//基本信息保存修改按钮
		basicPrivacy_jQ : $('.e_basicprivacy',basicInfo_jQ),//基本信息隐私
		basicPrivacys : ['privacySex','privacyBirthday','privacyLiveCityId','privacyHometownCityId']//基本信息隐私对应项
	};
	//游戏信息
	var gameInfo_jQ = $('.e_gameinfo',_elm);
	var gameInfo = {
		area_jQ : $('.e_area',gameInfo_jQ),   //大区
		career_jQ : $('.e_career',gameInfo_jQ), //职业
		school_jQ : $('.e_school',gameInfo_jQ), //门派
		gangs_jQ : $('.e_gangs',gameInfo_jQ), //帮会
		gameSave_jQ : $('.e_gamesave',gameInfo_jQ),//游戏信息提交按钮
		gamePrivacy_jQ : $('.e_gameprivacy',gameInfo_jQ),//游戏信息隐私
		gamePrivacys : ['privacyArea','privacyProfessionDictId','privacyGangDictId','privacyGameGang']//游戏信息隐私对应项
	}
	
	//个人信息
	var selfInfo_jQ = $(".e_selfinfo",_elm);
	var selfInfo = {
		star_jQ : $('.e_star',selfInfo_jQ),   //星座
		blood_jQ : $('.e_bloodtype',selfInfo_jQ), //血型
	    love_jQ : $('.e_love',selfInfo_jQ),   //感情
		idol_jQ : $('.e_idol',selfInfo_jQ),   //偶像
		wish_jQ : $('.e_wish',selfInfo_jQ),   //心愿
		motto_jQ : $('.e_motto',selfInfo_jQ), //座右铭
		music_jQ : $('.e_music',selfInfo_jQ), //喜欢音乐
		hobby_jQ : $('.e_hobby',selfInfo_jQ), //兴趣爱好
		introduction_jQ : $('.e_introduction',selfInfo_jQ), //个人介绍
		selfSave_jQ : $('.e_selfsave',selfInfo_jQ),//个人信息提交按钮
		selfPrivacy_jQ : $('.e_selfprivacy',selfInfo_jQ),//个人信息隐私
		selfPrivacys : ['privacyConstellation','privacyBloodType','privacyLoveStatus','privacyIdol','privacyWish','privacyMotto','privacyEnjoyMusic','privacyHobby','privacyIntroductions']//个人信息隐私对应项
	}
	
	//联系方式
	var relateInfo_jQ = $(".e_relateinfo",_elm);
	var relateInfo = {
		qq_jQ : $('.e_qq',relateInfo_jQ), // QQ
		phone_jQ : $('.e_phone',relateInfo_jQ), //手机
		site_jQ : $('.e_site',relateInfo_jQ), // 个人网站
		relateSave_jQ : $('.e_relatesave',relateInfo_jQ),//联系信息提交按钮
		relatePrivacy_jQ : $('.e_relateprivacy',relateInfo_jQ),//联系信息隐私
		relatePrivacys : ['privacyUserQq','privacyMobile','privacyHomePage']//联系信息隐私对应项
	}
	
	//所有隐私项
	var privacy_jQs = [basicInfo.basicPrivacy_jQ,gameInfo.gamePrivacy_jQ,selfInfo.selfPrivacy_jQ,relateInfo.relatePrivacy_jQ];
	var privacys = [basicInfo.basicPrivacys,gameInfo.gamePrivacys,selfInfo.selfPrivacys,relateInfo.relatePrivacys];
	
	
	var liveP;//现居住省市控件
	var homeP;//家乡省市控件
	var dateP;//日期控件
	
	
	//初始化下拉框信息
	var initializeSelect = function(_areaList){
		//加载时间控件
		dateP = new datePlugin(basicInfo.birth_year_jQ,basicInfo.birth_month_jQ,basicInfo.birth_date_jQ);
		//加载大区
		//加载地区控件
		liveP = new regionPlugin(basicInfo.liveProvince_jQ,basicInfo.liveCity_jQ);
		homeP = new regionPlugin(basicInfo.homeProvince_jQ,basicInfo.homeCity_jQ);
		//得到大区信息
		for(var areaId in _areaList){
			var areaOption_jQ = $('<option>',{'value':areaId,'text':_areaList[areaId]});
			gameInfo.area_jQ.append(areaOption_jQ);
		}	
		//加载门派,职业,两者一样
		var schools = SiteUtil.getGangDicts();
		for(var schoolId in schools){
			var schoolOption_jQ = $('<option>',{'value':schoolId,'text':schools[schoolId]});
			gameInfo.school_jQ.append(schoolOption_jQ);
			gameInfo.career_jQ.append(schoolOption_jQ.clone());
		}
		//加载星座
		var stars = SiteUtil.getConstellations();
		for(var starId in stars){
			var starOption_jQ = $('<option>',{'value':starId,'text':stars[starId]});
			selfInfo.star_jQ.append(starOption_jQ);
		}
		//加载血型
		var bloods = SiteUtil.getBloods();
		for(var bloodId in bloods){
			var bloodOption_jQ = $('<option>',{'value':bloodId,'text':bloods[bloodId]});
			selfInfo.blood_jQ.append(bloodOption_jQ);
		}
		//加载感情
		var loves = SiteUtil.getLoveStatus();
		for(var loveId in loves){
			var loveOption_jQ = $('<option>',{'value':loveId,'text':loves[loveId]});
			selfInfo.love_jQ.append(loveOption_jQ);
		}
	}
	
	//初始化所有隐私动作
	var initPrivacyAct = function(){
		for(var i = 0;i < privacy_jQs.length;i++){
			handlePrivacy(privacy_jQs[i]);
		};
		//处理隐私按私操作
		function handlePrivacy(ele_jQ){
			//点击隐私按钮时展开菜单
			ele_jQ.click(function(){
				ele_jQ.find('div').hide();
				$(this).find('div').show();
			});
			//鼠标移开时收起菜单
			ele_jQ.hover(function(){},function(){
				$(this).find('div').hide();
			});
			//点击隐私选项时隐藏菜单
			ele_jQ.find('a').click(function(){
				$(this).parent('div').slideUp(1).prev('em').removeClass().addClass('status').addClass($(this).attr('class')).html($(this).html()).attr('name',$(this).attr('class'));
			});
		}
	}
	
	//初始化隐私
	var initPrivacy = function(jdata){
		var data = $.parseJSON(jdata);
		if(data){
			//如果有隐私设置的话，初始化隐私          
			console.log(data);
			var basicdatas = [data.PSex,data.PBirthday,data.PLiveCityId,data.PHometownCityId];
			var gamedatas = [data.PArea,data.PProfessionDictId,data.PGangDictId,data.PGameGang];
			var selfdatas = [data.PConstellation,data.PBloodType,data.PLoveStatus,data.PIdol,data.PWish,data.PMotto,data.PEnjoyMusic,data.PHobby,data.PIntroductions];
			var relatedatas = [data.PUserQq,data.PMobile,data.PHomePage];
			var pdatas = [basicdatas,gamedatas,selfdatas,relatedatas];
			for(var i = 0;i < pdatas.length;i++){
				for(var j = 0;j < pdatas[i].length;j++){
					//改变em的class,text,name
					(pdatas[i][j] || pdatas[i][j]==0) && privacy_jQs[i].eq(j).find('em').removeClass().addClass('status').addClass(reverPrivacy[pdatas[i][j]]).attr('name',reverPrivacy[pdatas[i][j]]).html(privacyText[pdatas[i][j]]);
				}
			}
		}else{
			//没有隐私设置的话，全部设为完全公开
			for(var i = 0;i < privacy_jQs.length;i++){
				privacy_jQs[i].find('em').attr('name','all');
			}
		}
	}
	
	//初始化信息头点击收起动作与取消按钮点击收起动作
	var initInfoBar = function(){
		infoBar_jQ.click(
			function(){
				var ul = $(this).parent().next('ul');
				if(ul.is(':visible')){
					$(this).removeClass('active');
					ul.slideUp('fast');
				}else{
					$(this).addClass('active');
					ul.slideDown('fast')
				}
			}
		);
		cancel_jQ.each(function(index){
			$(this).click(function(){
					$(this).closest('ul').slideUp('fast');
					infoBar_jQ.eq(index).removeClass('active');
			});
		})
	}
	
	//初始化用户的已有信息
	var initialize = function(_areaList){
		//加载下拉框信息
		initializeSelect(_areaList);
		//得到用户的信息
		Backend.findUserData('',
		  function(_resp){
				if(Backend.dataValidate(_resp)){
					var data = _resp.data;
					//日期控件
					//console.log('data.birth:'+data.birthday);
					data.birthday && dateP.init(data.birthday.substr(0,4),data.birthday.substr(4,2),data.birthday.substr(6,2));
					//初始化省份
					//console.log('data.homeCity:'+data.hometownCityId);
					data.hometownCityId && homeP.init('', data.hometownCityId);
					//console.log('data.liveCIty:'+data.liveCityId);
					data.liveCityId && liveP.init('',data.liveCityId);
					//初始化性别
					//console.log('data.sex:'+data.sex);
					var sextemp = data.sex == '女'? 0 : 1;
					data.sex && basicInfo.sex_jQ.eq(1-sextemp).attr('checked','checked');
					//初始化大区
					//console.log('data.area:'+data.area);
					data.area && gameInfo.area_jQ.val(''+data.area);
					//初始化职业
					//console.log('data.userProfession:'+data.userProfession);
					data.userProfession && gameInfo.career_jQ.val(data.userProfession);
					//初始化门派
					//console.log('data.gangDictId:'+data.gangDictId);
					data.gangDictId && gameInfo.school_jQ.val(data.gangDictId);
					//初始化帮会
					//console.log('data.gameGang:'+data.gameGang);
					data.gameGang  &&　gameInfo.gangs_jQ.val(data.gameGang);
					//初始化星座
					//console.log('data.constellation:'+data.constellation);
					data.constellation &&  selfInfo.star_jQ.val(data.constellation);
					//初始化血型
					//console.log('data.bloodType:'+data.bloodType);
					data.bloodType && selfInfo.blood_jQ.val(data.bloodType);
					//初始化感情  
					//console.log('data.loveStatus:'+data.loveStatus);
					data.loveStatus && selfInfo.love_jQ.val(data.loveStatus);
					//初始化偶像  
					//console.log('data.idol:'+data.idol);
					data.idol && selfInfo.idol_jQ.val(data.idol); 
					//初始化心愿  
					//console.log('data.wish:'+data.wish);
					data.wish && selfInfo.wish_jQ.val(data.wish);
					//初始化座右铭 
					//console.log('data.motto:'+data.motto);
					data.motto && selfInfo.motto_jQ.val(data.motto);
					//初始化喜欢音乐
					//console.log('data.enjoyMusic:'+data.enjoyMusic);
					data.enjoyMusic && selfInfo.music_jQ.val(data.enjoyMusic);
					//初始化兴趣爱好
					//console.log('data.hobby:'+data.hobby);
					data.hobby && selfInfo.hobby_jQ.val(data.hobby);
					//初始化个人介绍
					//console.log('data.introductions:'+data.introductions);
					data.introductions && selfInfo.introduction_jQ.val(data.introductions);
					//初始化qq
					//console.log('data.userQq:'+data.userQq);
					data.userQq && relateInfo.qq_jQ.val(data.userQq);
					//初始化手机
					//console.log('data.mobile:'+data.mobile);
					data.mobile && relateInfo.phone_jQ.val(data.mobile);
					//初始化主页
					//console.log('data.homePage:'+data.homePage);
					data.homePage && relateInfo.site_jQ.val(data.homePage);
					
					//初始化隐私
					initPrivacy(data['secrecyPolicy']);
				}
		});
	}
	
	
	//提交信息 
	var submitInfo = function(){
		
		//基本信息保存
		basicInfo.basicSave_jQ.click(function(){
			var data = {};
			var birthday = '';
			var home = '';
			var live = '';
			basicInfo.birth_year_jQ.val() && (birthday+=basicInfo.birth_year_jQ.val());
			basicInfo.birth_month_jQ.val() && (birthday+=basicInfo.birth_month_jQ.val() >9 ?  basicInfo.birth_month_jQ.val() : '0'+basicInfo.birth_month_jQ.val());
			basicInfo.birth_date_jQ.val() && (birthday+=basicInfo.birth_date_jQ.val()>9 ?  basicInfo.birth_date_jQ.val() : '0'+basicInfo.birth_date_jQ.val());
			basicInfo.homeProvince_jQ.val() && (home = basicInfo.homeProvince_jQ.val());
			basicInfo.homeCity_jQ.val() && (home = basicInfo.homeCity_jQ.val()) ;
			basicInfo.liveProvince_jQ.val() && (live = basicInfo.liveProvince_jQ.val()); 
			basicInfo.liveCity_jQ.val() && (live = basicInfo.liveCity_jQ.val());
			data['userInfo.sex'] = basicInfo.sex_jQ.filter(':checked').val();
			data['userInfo.birthday'] = birthday;
			data['userInfo.liveCityId'] = live;
			data['userInfo.hometownCityId'] = home;
			//得到基本信息隐私
			for(var i = 0;i < basicInfo.basicPrivacy_jQ.length;i++){
				data['privacy.'+basicInfo.basicPrivacys[i]] = privacy[basicInfo.basicPrivacy_jQ.eq(i).find('em').attr('name')];
				console.log('privacy.'+basicInfo.basicPrivacys[i]+':'+data['privacy.'+basicInfo.basicPrivacys[i]]);
			}
			//将其它隐私部分设为-1
			markSave(basicInfo.basicPrivacys,data);
			//开始保存信息
			Backend.modifyUserInfo(data,function(_resp){
				alert(_resp.result);
			});
		});
		//游戏信息保存
		gameInfo.gameSave_jQ.click(function(){
			var data = {};
			data['userInfo.area'] = gameInfo.area_jQ.val();
			data['userInfo.userProfession'] = gameInfo.career_jQ.val();
			data['userInfo.gangDictId'] = gameInfo.school_jQ.val();
			data['userInfo.gameGang'] = gameInfo.gangs_jQ.val();
			//得到游戏信息隐私
			for(var i = 0;i < gameInfo.gamePrivacy_jQ.length;i++){
				data['privacy.'+gameInfo.gamePrivacys[i]] = privacy[gameInfo.gamePrivacy_jQ.eq(i).find('em').attr('name')];
			}
			//将其它隐私部分设为-1
			markSave(gameInfo.gamePrivacys,data);
			//开始保存信息
			Backend.modifyUserInfo(data,function(_resp){
				alert(_resp.result);
			});
		});
		//个人信息保存
		selfInfo.selfSave_jQ.click(function(){
			var data = {};
			data['userInfo.constellation'] = selfInfo.star_jQ.val();
			data['userInfo.bloodType'] = selfInfo.blood_jQ.val();
			data['userInfo.loveStatus'] = selfInfo.love_jQ.val();
			data['userInfo.idol'] = selfInfo.idol_jQ.val();
			data['userInfo.wish'] = selfInfo.wish_jQ.val();
			data['userInfo.motto'] = selfInfo.motto_jQ.val();
			data['userInfo.enjoyMusic'] = selfInfo.music_jQ.val();
			data['userInfo.hobby'] = selfInfo.hobby_jQ.val();
			data['userInfo.introductions'] = selfInfo.introduction_jQ.val();
			//得到个人信息隐私
			for(var i = 0;i < selfInfo.selfPrivacy_jQ.length;i++){
				data['privacy.'+selfInfo.selfPrivacys[i]] = privacy[selfInfo.selfPrivacy_jQ.eq(i).find('em').attr('name')];
			}
			//将其它隐私部分设为-1
			markSave(selfInfo.selfPrivacys,data);
			//开始保存信息
			Backend.modifyUserInfo(data,function(_resp){
				alert(_resp.result);
			});
		});
		//联系方保存
		relateInfo.relateSave_jQ.click(function(){
			var data = {};
			var isright = true;
			var regx = /^(http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-.\/?%&=]*))*?$/g;
			if(!regx.test(relateInfo.site_jQ.val())){
				isright = false;
				relateInfo.site_jQ.next('span').text('请输入正确的个人网站地址!');
			}else{
				relateInfo.site_jQ.next('span').text('');
			}
			var phoneRegx = /^(1(3|4|5|8)([0-9]{9}))*$/g;
			if(!phoneRegx.test(relateInfo.phone_jQ.val())){
				relateInfo.phone_jQ.next('span').text('请输入正确的手机号!');
			}else{
				relateInfo.phone_jQ.next('span').text('');
			}
			var qqRegx = /^\d*$/g;
			if(!qqRegx.test(relateInfo.qq_jQ.val())){
				relateInfo.qq_jQ.next('span').text('请输入正确的qq号');
			}else{
				relateInfo.qq_jQ.next('span').text('');
			}
			data['userInfo.userQq'] = relateInfo.qq_jQ.val();
			data['userInfo.mobile'] = relateInfo.phone_jQ.val();
			data['userInfo.homePage'] = relateInfo.site_jQ.val();
			//得到基本信息隐私
			for(var i = 0;i < relateInfo.relatePrivacy_jQ.length;i++){
				data['privacy.'+relateInfo.relatePrivacys[i]] = privacy[relateInfo.relatePrivacy_jQ.eq(i).find('em').attr('name')];
			}
			//将其它隐私部分设为-1
			markSave(relateInfo.relatePrivacys,data);
			//开始保存信息
			if(isright){
				Backend.modifyUserInfo(data,function(_resp){
					alert(_resp.result);
				});
			}
		});
		//保存一部分时，给其它部分的隐私值设为-1，让后台判断是否保存
		function markSave(saveData,data){
			for(var i = 0;i < privacys.length;i++){
				if(privacys[i] != saveData){
					for(var j = 0;j < privacys[i].length;j++){
						data['privacy.'+privacys[i][j]] = -1;
					}
				}
			}
		}
	}
	
	
	//日期控件
	var datePlugin = function(year_container,month_container,date_container){
		this.year_container = year_container;
		this.month_container = month_container;
		this.date_container = date_container;
		this.load();
	} 
	
	datePlugin.prototype = {
		
		init : function(cyear,cmonth,cdate){
			
			//选中默认年份
			if(cyear){
				this.year_container.find('[value='+cyear+']').attr('selected','selected');
				this.month_container.removeAttr('disabled');
			}else{
				this.month_container.attr('disabled','true');
			}
			
			//选中默认月份
			if(cmonth){
				this.month_container.find('option').eq(cmonth).attr('selected','selected');
				this.addDate();
				this.date_container.removeAttr('disabled');
			}else{
				this.date_container.attr('disabled','true');
			}
			//选中默认日期
			if(cdate){
				this.addDate();
				this.date_container.find('option').eq(cdate).attr('selected','selected');
			}
			
		},
		load : function(){
			var _self = this;
			//添加从1903年开始的生日年份下拉
			this.addYear();
			//加载月份
			this.addMonth();
			//禁用月份与日期
			this.month_container.attr('disabled','true');
			this.date_container.attr('disabled','true');
			//当年份改变的时候加载月份
			this.year_container.bind("change",function(){
				//年份有值的时候
				if(_self.year_container.val()){
					_self.month_container.removeAttr('disabled');
					_self.reloadDate();
				}else{
				//年份无值的时候
					//清空日期
					_self.clearDate();
					//将月份恢复到选择状态
					_self.month_container.find('option').eq(0).attr('selected','selected');
					_self.month_container.attr('disabled','true');
					_self.date_container.attr('disabled','true');
					
				}
					
			});
			//当月份改变的时候加载日期
			this.month_container.bind("change",function(){
				_self.reloadDate();
			});
		},
		addYear : function(){
			var now = new Date();
			for(var i = +now.getFullYear();i >=1903 ;i--){
				var yearOption_jQ = $('<option>',{'value':i,'text':i});
				this.year_container.append(yearOption_jQ);
			}
		},
		
		addMonth : function(){
			for(var i = 1;i <= 12;i++){
				var monthOption_jQ = $('<option>',{'value':i,'text':i});
				this.month_container.append(monthOption_jQ);
			}
		},
		
		addDate : function(){
			if(basicInfo.birth_month_jQ.val()){
				var date = new Date(this.year_container.val(),this.month_container.val(),0);
				for(var i = 1;i <= date.getDate();i++){
					var dateOption_jQ = $('<option>',{'value':i,'text':i});
					this.date_container.append(dateOption_jQ);
				}
				this.date_container.removeAttr('disabled');
			}else{
				this.date_container.attr('disabled','true');
			}
		},
		
		reloadDate : function(){
			var nowdate = basicInfo.birth_date_jQ.val();
			this.clearDate();
			this.addDate();
			this.date_container.val(nowdate);
		},
		
		clearDate : function(){
			this.date_container.empty();
			this.date_container.append($('<option>',{'value':'','text':'选择日期'}));
		}
	} 
	
	//地区控件
	var regionPlugin = function(provinceContainer,cityContainer){
		this.provinceContainer = provinceContainer;
		this.cityContainer = cityContainer;
		this.load();
	}
	
	regionPlugin.prototype = {
		init : function(provinceId,cityId){
			
			//加载默认城市
			if(cityId){
				this.cityContainer.removeAttr('disabled');
				var city = province_city_map.city[cityId];
				if(city){
					var province = city.parentId;
					this.provinceContainer.val(province);
					this.addCity();
					this.cityContainer.val(cityId);
				}
			}else{
				//加载默认省份
				if(provinceId){
					this.cityContainer.removeAttr('disabled');
					this.provinceContainer.val(provinceId);
				}else{
					this.cityContainer.attr('disabled','true');
				}
			}
		},
		load : function(){
			//加载省份
			var _self = this;
			this.addProvince();
			//加载市区
			this.provinceContainer.bind("change",function(){
				if($(this).val()){
					_self.reloadCity(this.provinceContainer);
					_self.cityContainer.removeAttr('disabled');
				}else{
					_self.clearCity();
					_self.cityContainer.attr('disabled','true');
				}
			});
			//禁用市区
			this.cityContainer.attr('disabled','true');
		},
		addProvince : function(){
			for(var province in province_city_map.province){
				var provinceOption_jQ = $('<option>',{'value':province,'text':province_city_map.province[province]});
				this.provinceContainer.append(provinceOption_jQ);
			}
		},
		addCity : function(){
			if(this.provinceContainer.val()){
				var cityArr = province_city_map.relation[this.provinceContainer.val()];
				for(var i = 0;i< cityArr.length;i++){
					var cityOption_jQ = $('<option>',{'value':cityArr[i],'text':province_city_map.city[cityArr[i]].name});
					this.cityContainer.append(cityOption_jQ);
				}
			}
		},
		reloadCity : function(){
			this.clearCity();
			this.addCity();
		},
		clearCity : function(){
			this.cityContainer.empty();
			this.cityContainer.append($('<option>',{'text':'选择城市'}));
		}
	}
	
	
	//逻辑加载
	initInfoBar();
	initPrivacyAct();
	SiteUtil.getServerZone(function(_areaList){//加载服务器
		SiteUtil.getRegion(function(){//加载大区
			initialize(_areaList);
		})
	})	
	submitInfo();
});